package pers.csw.lookBook.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.csw.lookBook.common.Res;
import pers.csw.lookBook.dto.LabelDto;
import pers.csw.lookBook.entity.BookLabel;
import pers.csw.lookBook.entity.Tag;
import pers.csw.lookBook.service.LabelService;
import pers.csw.lookBook.service.TagService;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
    @Autowired
    LabelService labelService;
    @Autowired
    TagService tagService;

    /**
     * 通过书籍id查询书籍所拥有的所有标签信息
     * @param bookId 书籍id
     * @return 返回标签集合
     */
    @GetMapping("/getLabelByBookId/{bookId}")
    public Res getLabelByBookId(@PathVariable Integer bookId){
        QueryWrapper<BookLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id",bookId);
        List<BookLabel> list = labelService.list(queryWrapper);
        List<LabelDto> labelDtos = new ArrayList<>();
        list.forEach(label->{
            LabelDto l = new LabelDto();
            Tag byId = tagService.getById(label.getTagId());
            l.setId(label.getId());
            l.setName(byId.getName());
            labelDtos.add(l);
        });
        return Res.success(labelDtos);
    }
}
